package com.situ.orderingsystem.dao.impl;

import com.situ.orderingsystem.dao.FoodDao;
import com.situ.orderingsystem.global.Global;
import com.situ.orderingsystem.model.Employee;
import com.situ.orderingsystem.model.Food;
import com.situ.orderingsystem.model.FoodSearchBean;
import com.situ.orderingsystem.model.Kind;
import com.situ.orderingsystem.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    private  final JdbcTemplate template = new JdbcTemplate(Global.getDataSource());

    @Override
    public List<Food> findAll(FoodSearchBean es, PaginateInfo pi) {
        String countsql ="select count(0) from food t1";
        String sql = "select t1.id ,t1.food_id,t1.`name`,t1.kind_id,t2.`name` as kindName,t1.price,t1.description from food t1 left JOIN kind t2 on t2.kind_id = t1.kind_id";
        List<Object> objectList = new ArrayList<>();

        StringBuilder whysql = new StringBuilder();
        if(es != null){
            whysql.append(" where 1 = 1 and t1.is_deleted = 0");
            if(es.getFoodId() != null){
                whysql.append(" and food_id = ?");
                objectList.add(es.getFoodId());
            }
            if(es.getKindId() != null){
                whysql.append(" and kind_id = ?");
                objectList.add(es.getKindId());
            }
            if(StringUtils.hasText(es.getName())){
                whysql.append(" and name like ?");
                objectList.add("%"+es.getName()+"%");
            }
            if(es.getPrice() != 0){
                whysql.append(" and price = ?");
                objectList.add(es.getPrice());
            }
            if(StringUtils.hasText(es.getDescription())){
                whysql.append(" and description like ?");
                objectList.add("%"+es.getDescription()+"%");
            }
        }
        countsql += whysql.toString();

        Long count = template.queryForObject(countsql,Long.class,objectList.toArray());
        pi.setCount(count);
        whysql.append(" limit ?, ?");
        objectList.add(pi.getOffset());
        objectList.add(pi.getLimit());
        sql+=whysql;
        List<Food> list = template.query(sql,new BeanPropertyRowMapper<>(Food.class),objectList.toArray());
//        System.out.println(list.get(0).toString());
        return list;

    }
    public List<Food> findAll() {
        String sql = "select id,food_id,kind_id,`name`,price,description from food";
        List<Food> list = template.query(sql,new BeanPropertyRowMapper<>(Food.class));
        return list;
    }
    @Override
    public List<Food> findCombo() {
       String sql ="select food_id,kind_id,`name` from food";
        List<Food> list = template.query(sql,new BeanPropertyRowMapper<>(Food.class));
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql ="update food set is_deleted = 1 where id = ?";
        int rows = template.update(sql,id);
        return  rows>0;
    }

    @Override
    public int add(Food food) {
        String sql = "insert into food" +
                "(food_id,kind_id,`name`,price,description)" +
                "values (?,?,?,?,?)";
        int rows = template.update(sql,food.getFoodId(),food.getKindId(),food.getName(),food.getPrice(),food.getDescription());
        return rows;
    }

    @Override
    public Food searchById(Integer id) {
        String sql = "select food_id,kind_id,`name`,price,description from food where id = ?";
        List<Food> emp = template.query(sql,new BeanPropertyRowMapper<>(Food.class),id);
        return emp.get(0);
    }

    @Override
    public int update(Food food) {
        String sql ="update food set food_id = ? , kind_id = ?, `name` = ? ,price = ?,description = ? where id = ?";
        int rows = template.update(sql,food.getFoodId(),food.getKindId(),food.getName(),food.getPrice(),food.getDescription(),food.getId());
        return  rows ;
    }
}
